package com.yixinsoft.basic.model.manage.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * Created by hqy on 2016/06/05
 */
@Table(name = "sys_user")
public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id; //编号
	
	@Column(name = "organization_id")
	private Long organizationId;	//所属公司
	
	private String username;	//用户名
	
	private String password;	//密码
	
	private String salt;		//加密密码的盐
	
	@Column(name = "role_ids")
	private String roleIds;	
	
	@Transient
	private List<Long> roles;	//拥有的角色列表
	
	private Boolean locked = Boolean.FALSE;

	public User() {
		super();
	}

	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(Long organizationId) {
		this.organizationId = organizationId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}
	
	public String getCredentialsSalt() {
		return username + salt;
	}

	public List<Long> getRoles() {
		if (roles == null) {
			roles = new ArrayList<>();
		}
		return roles;
	}

	public void setRoles(List<Long> roles) {
		this.roles = roles;
	}
	
	public String getRoleIds() {
		if (CollectionUtils.isEmpty(roles)) {
			return "";
		}
		StringBuilder s = new StringBuilder();
		for (Long roleId : roles) {
			s.append(roleId);
			s.append(",");
		}
		return s.toString();
	}

	public void setRoleIds(String roleIds) {
		if (StringUtils.isEmpty(roleIds)) {
			return;
		}
		String[] roleIdStrs = roleIds.split(",");
		for (String roleIdStr : roleIdStrs) {
			if (StringUtils.isEmpty(roleIdStr)) {
				continue;
			}
			getRoles().add(Long.valueOf(roleIdStr));
		}
	}
	
	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
